package cn.tqfeiyang.smartadmin.business.organization.service;

import cn.tqfeiyang.smartadmin.business.organization.entity.DepartmentEntity;

import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import jakarta.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * 部门列表服务
 *
 * @author tqfeiyang
 * @since 2024-10-18
 */
@Service
public class DepartmentListService {

    /**
     * 部门缓存服务
     */
    @Resource
    private DepartmentCacheService departmentCacheService;

    /**
     * 查询部门列表
     *
     * @return 部门列表
     */
    public List<DepartmentEntity> getDepartmentList() {
        return departmentCacheService.getDepartmentList();
    }

    /**
     * 查询部门map
     *
     * @return 部门map
     */
    public Map<Long, DepartmentEntity> getDepartmentMap() {
        return departmentCacheService.getDepartmentMap();
    }

    /**
     * 根据部门id查询部门信息
     *
     * @param departmentId 部门id
     * @return 部门信息，如果不存在则返回null
     */
    public DepartmentEntity findDepartmentByDepartmentId(Long departmentId) {
        Assert.notNull(departmentId, "departmentId不能为空");
        return departmentCacheService.findDepartmentByDepartmentId(departmentId);
    }


    /**
     * 根据名称查询部门信息
     *
     * @param name 名称
     * @return 部门信息，如果不存在则返回null
     */
    public DepartmentEntity findDepartmentByName(String name) {
        Assert.notNull(name, "name不能为空");
        return getDepartmentList().stream()
                .filter(entity -> name.equals(entity.getName()))
                .findFirst()
                .orElse(null);
    }
}
